package com.tang.application;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tang.model.Person;
import com.tang.model.Student;
import com.tang.model.Teacher;
import com.tang.util.Logger;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:tibby tang
 * Created on 21/1/15.
 * Email:tangjian19900607@gmail.com
 * QQ:562980080
 */
public class Application {
    private Gson gson = new Gson();
    public static void main(String[] args){
        Logger.setDebug(true);
        Logger.setTag("Gson");
        Application application = new Application();
        application.parseList1();
    }
    public void parseStudent(){
        Student student = new Student();
        student.setAge(20);
        student.setName("zhangsan");

        Logger.log("转换为json");
        String json = gson.toJson(student);
        Logger.log(json);

        Logger.log("解析Json为对象");
        Student student1 = gson.fromJson(json,Student.class);
        Logger.log("stu.name:"+student1.getName());
        Logger.log("stu.age:"+student1.getAge());

    }

    public void parseList1(){
        ArrayList<Student> list = new ArrayList<Student>();

        Student student1 = new Student();
        student1.setName("aaa");
        student1.setAge(21);
        Teacher teacher1 = new Teacher();
        teacher1.setName("zhangsan1");
        teacher1.setAge("21");
        student1.setTeacher(teacher1);
        Person person1 = new Person();
        person1.setColor("blue");
        person1.setMale("male");
        Map<String,Person> map1 = new HashMap<String,Person>();
        map1.put("person1",person1);
        student1.setExtra(map1);

        Student student2 = new Student();
        student2.setName("bbb");
        student2.setAge(21);
        Teacher teacher2 = new Teacher();
        teacher2.setName("zhangsan2");
        teacher2.setAge("22");
        student2.setTeacher(teacher2);
        Person person2 = new Person();
        person2.setColor("yellow");
        person2.setMale("female");
        Map<String,Person> map2 = new HashMap<String,Person>();
        map2.put("person1",person2);
        student2.setExtra(map2);

        Student student3 = new Student();
        student3.setName("ccc");
        student3.setAge(21);
        Teacher teacher3 = new Teacher();
        teacher3.setName("zhangsan3");
        teacher3.setAge("23");
        student3.setTeacher(teacher3);
        Person person3 = new Person();
        person3.setColor("yellow");
        person3.setMale("female");
        Map<String,Person> map3 = new HashMap<String,Person>();
        map3.put("person1",person3);
        student3.setExtra(map3);

        Student student4 = new Student();
        student4.setName("ddd");
        student4.setAge(21);
        Teacher teacher4 = new Teacher();
        teacher4.setName("zhangsan4");
        teacher4.setAge("24");
        student4.setTeacher(teacher4);
        Person person4 = new Person();
        person4.setColor("yellow");
        person4.setMale("female");
        Map<String,Person> map4 = new HashMap<String,Person>();
        map4.put("person1",person4);
        student4.setExtra(map4);

        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);

        Logger.log("转换为json");
        String json = gson.toJson(list);
        Logger.log(json);

        Logger.log("解析Json为对象");
        Type type = new TypeToken<List<Student>>(){}.getType();
        ArrayList<Student> students = gson.fromJson(json,type);

        for (Student stu:students){
            Logger.log("stu.name:"+stu.getName());
            Logger.log("stu.age:"+stu.getAge());
            Logger.log("stu.teach.age:"+stu.getTeacher().getAge());
            Logger.log("stu.teach.name:"+stu.getTeacher().getName());
            Logger.log("stu.extra.person.color:"+stu.getExtra().get("person1").getColor());
        }
    }

}
